package ru.ksu.ktk.diploma.sdi.search;
import java.util.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class DisambiguatingSearch extends SearchImpl
{
    public DisambiguatingSearch()
    {

    }

    public static void main(String[] args)
    {
        DisambiguatingSearch engine = new DisambiguatingSearch();

        engine.init();
        System.out.println("init OK.");

        engine.loadRUSAAT();
        System.out.println("RUSAAT OK.");

        String qString = " оружие фото ";

        ArrayList<Individual> terms = engine.prepareQuery(qString);
        System.out.println("query \"" + qString + "\" OK.");

        ArrayList<ArrayList<Individual>> ambiguousTerms = engine.checkQueryAmbiguity(terms);
        System.out.println("ambiguity check OK.");

        HashMap<List<Individual>, OntModel> queryResults = new HashMap<List<Individual>, OntModel>();

        queryResults.put(terms, engine.executeFastQueryWithAmbiguousTerms(ambiguousTerms));

        System.out.println("query execution OK.");

        engine.storeQueryResult(queryResults);

        System.out.println("Result set URL list:");
        for(OntModel o : queryResults.values())
        {
            for(String s :engine.listResultURLs(o))
            {
                System.out.println(s);
            }
        }
    }

    // поиск, основанный на индексе и учитывающий лексическую многозначность
    public OntModel executeFastQueryWithAmbiguousTerms(ArrayList<ArrayList<Individual>> termLists)
    {
        ArrayList<OntModel> modelsList = new ArrayList<OntModel>();
        for(ArrayList<Individual> terms : termLists)
        {
            ArrayList<OntModel> models = retrieveModelsFromIndex(terms);
            modelsList.add(applyFormulaOnModels(models, "JOIN"));

        }
        return applyFormulaOnModels(modelsList, "INTERSECT");
    }

    // проверяем многозначность списка понятий
    private ArrayList<ArrayList<Individual>> checkQueryAmbiguity(ArrayList<Individual> terms)
    {
        ArrayList<ArrayList<Individual>> result = new ArrayList<ArrayList<Individual>>();

        for(Individual term_A : terms)
        {
            ArrayList<Individual> senses = new ArrayList<Individual>();
            for(Individual term_B : terms)
            {
              /*  if(term_A != term_B)
                {*/
                    if(!senses.contains(term_A))
                    {
                        System.out.println("add A:" + term_A.getLocalName());
                        senses.add(term_A);
                    }
                    if(!term_A.getURI().equalsIgnoreCase(term_B.getURI()))
                    {
                        ExtendedIterator A_labels = term_A.listLabels("ru");
                        ExtendedIterator B_labels = term_B.listLabels("ru");
                        for(Object a : A_labels.toSet())
                        {
                            for(Object b : B_labels.toSet())
                            {
                                if(a.toString().equalsIgnoreCase(b.toString()))
                                {
                                    if(!senses.contains(term_B))
                                    {
                                        System.out.println("add B:" + term_B.getLocalName());
                                        senses.add(term_B);
                                        System.out.println(a.toString() + " = " + b.toString());
                                    }

                                }
                            }
                        }
                  //  }
                }
            }
            if(senses.size() > 0)
            {
                result.add(senses);
            }
        }
        return result;
    }

}
